int sum = 0;
int temp[50000];
void merge(int nums[], int begin, int mid, int end) {
    int k = 0;
    int i = begin;
    int j = mid + 1;
    while (i <= mid && j <= end) {
        if (nums[i] <= nums[j]) {
            temp[k++] = nums[i++];
        } else {
            sum += mid - i + 1;
            temp[k++] = nums[j++];
        }
    }
    while (i <= mid) {
        temp[k++] = nums[i++];
    }
    while (j <= end) {
        temp[k++] = nums[j++];
    }
    for (int t = 0; t < k; t++) {
        nums[begin++] = temp[t];
    }
}

void mergesort_c(int nums[], int begin, int end) {
    if (begin >= end) {
        return;
    }
    int mid = (begin + end) / 2;
    mergesort_c(nums, begin, mid);
    mergesort_c(nums, mid + 1, end);
    merge(nums, begin, mid, end);
}

int reversePairs(int* nums, int numsSize){
    sum = 0;
    mergesort_c(nums, 0, numsSize - 1);
    return sum;
}
